package com.example.lib_base.manager


import com.example.lib_base.model.User

class UserMangaer private constructor(){
    private var bmobManager = BmobManager.getInstance()
    var user: User? = null

    companion object{
        private var instance:UserMangaer? = null

        fun getInstance():UserMangaer{
            if(instance == null){
                synchronized(this){
                    if(instance == null){
                        instance = UserMangaer()
                    }
                }
            }
            return instance!!
        }
    }

    /**手机号登录*/
    fun loginByPhone(phone:String,pwd:String,onStart:()->Unit={}, onEnd:(Boolean)->Unit = {}){
        onStart()
        bmobManager.loginByPhone(phone,pwd){
            onEnd(it)
        }
    }

    /**手机号+验证码一键注册或登录*/
    fun registrationOrLogin(phone:String,code:String,onStart:()->Unit={}, onEnd:(Boolean)->Unit = {}){
        onStart()
        bmobManager.registrationOrLogin(phone,code){
            onEnd(it)
        }
    }

    /**用手机号修改密码*/
    fun resetPassword(pwd:String,code:String,onStart:()->Unit={}, onEnd:(Boolean)->Unit = {}){
        onStart()
        bmobManager.resetPassword(pwd,code){
            onEnd(it)
        }
    }

    /**申请验证码*/
    fun requestSMSCode(phone:String,onStart:()->Unit={}, onEnd:(Boolean)->Unit = {}){
        onStart()
        bmobManager.requestSMSCode(phone){
            onEnd(it)
        }
    }

    /**获取当前登录用户*/
    fun getCurrentUser() = bmobManager.getCurrentUser()

    /**是否有用户登录*/
    fun isLogin():Boolean{
        if(bmobManager.isLogin()){
            user = bmobManager.getCurrentUser()
            return true
        }

        return false
    }

    /**退出登录*/
    fun logOut(){
        bmobManager.logOut()
        user = null
    }



}